/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.bricket.web.common;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.AjaxPreprocessingCallDecorator;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.odlabs.wiquery.ui.themes.ThemeUiHelper.IconEnum;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
public abstract class AActionLink extends Panel {
	private final StringResourceModel message;

	public AActionLink(boolean ajax) {
		this(ajax, null);
	}

	public AActionLink(boolean ajax, StringResourceModel message) {
		super("item");
		this.message = message;
		if (ajax) {
			ajaxInit();
		} else {
			init();
		}
	}

	private void ajaxInit() {
		AjaxLink<Object> link = new AjaxLink<Object>("link") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				AActionLink.this.onClick(target);
			}
			@Override
			protected IAjaxCallDecorator getAjaxCallDecorator() {
				return new AjaxPreprocessingCallDecorator(super.getAjaxCallDecorator()) {
					@Override
					public CharSequence preDecorateScript(CharSequence script) {
						return message == null ? script : "if(!confirm('" + message.getString() + "')) return false;" + script;
					}
				};
			}
		};
		link.add(new Label("icon", "").add(new AttributeModifier("class", true, new Model<String>(BricketAjaxPanel.BUTTON_ICON_BASE_CLASS + getIcon().getCssClass()))));
		add(link);
	}

	private void init() {
		Link<Object> link = new Link<Object>("link") {
			@Override
			public void onClick() {
				AActionLink.this.onClick(null);
			}
		};
		if (message!=null) {
			link.add(new SimpleAttributeModifier("onclick", "return confirm('" + message.getString() + "');"));
		}
		link.add(new Label("icon", "").add(new AttributeModifier("class", true, new Model<String>(BricketAjaxPanel.BUTTON_ICON_BASE_CLASS + getIcon().getCssClass()))));
		add(link);
	}

	protected IAjaxCallDecorator getAjaxCallDecorator() {
		return null;
	}

	abstract public void onClick(AjaxRequestTarget target);

	abstract IconEnum getIcon();
}
